﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Biletomaty.Resources;
using Biletomaty.ViewModels;
using System.Device.Location;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using Windows.Devices.Geolocation;

namespace Biletomaty
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            App.ViewModel.LoadData();
            // Set the data context of the LongListSelector control to the sample data
            App.ViewModel.MainMap_SetItems += MainMap_SetItems;
            DataContext = App.ViewModel;

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void PhoneApplicationPage_OrientationChanged_1(object sender, OrientationChangedEventArgs e)
        {
            switch (e.Orientation)
            {
                case PageOrientation.LandscapeLeft:
                case PageOrientation.LandscapeRight:
                case PageOrientation.Landscape:
                    MainMap.Visibility = Visibility.Visible;
                    MainLongListSelector.Visibility = Visibility.Collapsed;
                    break;

                case PageOrientation.PortraitDown:
                case PageOrientation.PortraitUp:
                case PageOrientation.Portrait:
                    MainMap.Visibility = Visibility.Collapsed;
                    MainLongListSelector.Visibility = Visibility.Visible;
                    break;
            }
        }

        public void MainMap_SetItems(object sender, LocationEventArgs e)
        {
            Ellipse myCircle = new Ellipse();
            myCircle.Fill = new SolidColorBrush(Colors.Blue);
            myCircle.Height = 20;
            myCircle.Width = 20;
            myCircle.Opacity = 50;

            foreach (var item in e.Items)
            {
                //MapOverlay myLocationOverlay = new MapOverlay();
                //myLocationOverlay.Content = myCircle;
                //myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                //myLocationOverlay.GeoCoordinate = new GeoCoordinate(item.Location.Latitude, item.Location.Longitude);

                //// Create a MapLayer to contain the MapOverlay.
                //var locationLayer = new MapLayer();
                //locationLayer.Add(myLocationOverlay);

                //// Add the MapLayer to the Map.
                //MainMap.Layers.Add(locationLayer);
            }
        }

        private void MainMap_Loaded(object sender, RoutedEventArgs e)
        {


            try
            {
                GetLocation();
                // Create a small circle to mark the current location.
                Ellipse myCircle = new Ellipse();
                myCircle.Fill = new SolidColorBrush(Colors.Red);
                myCircle.Height = 20;
                myCircle.Width = 20;
                myCircle.Opacity = 50;

                MapOverlay myLocationOverlay = new MapOverlay();
                myLocationOverlay.Content = myCircle;
                myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
                myLocationOverlay.GeoCoordinate = CurrentLocation;

                MainMap.Center = CurrentLocation;

                // Create a MapLayer to contain the MapOverlay.
                var locationLayer = new MapLayer();
                locationLayer.Add(myLocationOverlay);

                // Add the MapLayer to the Map.
                MainMap.Layers.Add(locationLayer);
            }
            catch (ArgumentNullException err)
            {
                MessageBox.Show("Nie moge odnaleść twojej lokalizacji!");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private async void GetLocation()
        {
            // Get current location.
            Geolocator myGeolocator = new Geolocator();
            Geoposition myGeoposition = await myGeolocator.GetGeopositionAsync();
            Geocoordinate myGeocoordinate = myGeoposition.Coordinate;
            CurrentLocation = ConvertGeocoordinate(myGeocoordinate);
        }

        private GeoCoordinate ConvertGeocoordinate(Geocoordinate geocoordinate)
        {
            return new GeoCoordinate
            (
                geocoordinate.Latitude,
                geocoordinate.Longitude,
                geocoordinate.Altitude ?? Double.NaN,
                geocoordinate.Accuracy,
                geocoordinate.AltitudeAccuracy ?? Double.NaN,
                geocoordinate.Speed ?? Double.NaN,
                geocoordinate.Heading ?? Double.NaN
            );
        }

        public GeoCoordinate CurrentLocation { get; set; }
    }
}